{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter\n",
    "import tkinter.messagebox\n",
    "import random\n",
    "import threading\n",
    "import itertools\n",
    "import time\n",
    "\n",
    "root = tkinter.Tk()\n",
    "# 窗口标题\n",
    "root.title('随机提问')\n",
    "# 窗口初始大小和位置\n",
    "root.geometry('260x180+400+300')\n",
    "# 不允许改变窗口大小\n",
    "root.resizable(False, False)\n",
    "\n",
    "# 关闭程序时执行的函数代码，停止滚动显示学生名单\n",
    "def closeWindow():\n",
    "    root.flag = False\n",
    "    time.sleep(0.1)\n",
    "    root.destroy()\n",
    "\n",
    "root.protocol('WM_DELETE_WINDOW', closeWindow)\n",
    "\n",
    "# 模拟学生名单，可以加上数据库访问接口，从数据库中读取学生名单\n",
    "students = ['张三', '李四', '王五', '赵六', '周七', '钱八']\n",
    "\n",
    "# 变量，用来控制是否滚动显示学生名单\n",
    "root.flag = False\n",
    "\n",
    "def switch():\n",
    "    root.flag = True\n",
    "    # 随机打乱学生名单\n",
    "    t = students[:]\n",
    "    random.shuffle(t)\n",
    "    t = itertools.cycle(t)\n",
    "    \n",
    "    while root.flag:\n",
    "        # 滚动显示\n",
    "        lbFirst['text'] = lbSecond['text']\n",
    "        lbSecond['text'] = lbThird['text']\n",
    "        lbThird['text'] = next(t)\n",
    "        # 数字可以修改，控制滚动速度\n",
    "        time.sleep(0.1)\n",
    "\n",
    "def btnStartClick():\n",
    "    # 每次单击“开始”按钮启动新线程\n",
    "    t = threading.Thread(target=switch)\n",
    "    t.start()\n",
    "    btnStart['state'] = 'disabled'\n",
    "    btnStop['state'] = 'normal'\n",
    "\n",
    "btnStart = tkinter.Button(root,\n",
    "                          text='开始',\n",
    "                          command=btnStartClick)\n",
    "btnStart.place(x=30, y=10, width=80, height=20)\n",
    "\n",
    "def btnStopClick():\n",
    "    # 单击“停”按钮结束滚动显示\n",
    "    root.flag = False\n",
    "    time.sleep(0.3)\n",
    "    tkinter.messagebox.showinfo('恭喜',\n",
    "                                '本次中奖：' + lbSecond['text'])\n",
    "    btnStart['state'] = 'normal'\n",
    "    btnStop['state'] = 'disabled'\n",
    "\n",
    "btnStop = tkinter.Button(root,\n",
    "                         text='停',\n",
    "                         command=btnStopClick)\n",
    "btnStop['state'] = 'disabled'\n",
    "btnStop.place(x=150, y=10, width=80, height=20)\n",
    "\n",
    "# 用来滚动显示学生名单的3个Label组件\n",
    "# 可以根据需要进行添加，但要修改上面的线程函数代码\n",
    "lbFirst = tkinter.Label(root, text='')\n",
    "lbFirst.place(x=80, y=60, width=100, height=20)\n",
    "\n",
    "# 红色Label组件，表示中奖名单\n",
    "lbSecond = tkinter.Label(root, text='')\n",
    "lbSecond['fg'] = 'red'\n",
    "lbSecond.place(x=80, y=90, width=100, height=20)\n",
    "\n",
    "lbThird = tkinter.Label(root, text='')\n",
    "lbThird.place(x=80, y=120, width=100, height=20)\n",
    "\n",
    "# 启动tkinter主程序\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
